//
//  CoinImageView.swift
//  Crypto
//
//  Created by DaleGuo on 2023/9/22.
//

import SwiftUI

struct CoinImageView: View {
    @StateObject var vm: CoinImageViewModel

    init(coin: CoinModel) {
        // 因为不能对StateObject直接改动 （vm = ~~~❌） 所以要用_访问实例
        _vm = StateObject(wrappedValue: CoinImageViewModel(coin: coin))
    }

    var body: some View {
        ZStack {
            if let image = vm.image {
                Image(uiImage: image)
                    .resizable()
                    .scaledToFit()
            } else if vm.isLoading {
                ProgressView()
            } else {
                // 即没有加载也没有图片  说明请求失败
                Image(systemName: "questionmark")
                    .foregroundStyle(Color.theme.secondaryText)
            }
        }
    }
}

#Preview {
    CoinImageView(coin: DeveloperPreview.instance.coin)
}
